<template>
  <div class="login-container">
    <el-form ref="loginFormRef" :model="loginForm" :rules="rules">
      <el-form-item prop="username">
        <el-input
          v-model="loginForm.username"
          prefix-icon="el-icon-user"
          placeholder="用户名"
        />
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          v-model="loginForm.password"
          show-password
          prefix-icon="el-icon-lock"
        />
      </el-form-item>

      <el-button
        type="primary"
        native-type="submit"
        @click="submitForm"
        style="width: 100%"
        :loading="submitting"
        >登录</el-button
      >
    </el-form>
  </div>
</template>
<script lang="ts">
import { defineComponent, ref, unref, onMounted } from "vue";
import {useStore} from 'vuex'
import { useRouter } from 'vue-router'

export default defineComponent({
  name: "login",
  setup() {
    const loginForm = ref({
      username: "",
      password: "",
    });
    const loginFormRef = ref();
    const submitting = ref(false);
    const rules = {
      username: [{ required: true }],
      password: [{ required: true }],
    };
     const store = useStore();
      const router = useRouter()
    const submitForm = () => {
      const form = unref(loginFormRef);
      if (form) {
        form.validate((valid: boolean) => {
          if (valid) {
            submitting.value = true;
            setTimeout(() => {
              store.commit('login',loginForm.value.username)
              submitting.value = false;
              router.push('./')
            }, 1000);
          }
        });
      }
    };
    onMounted(() => {
      console.dir(loginFormRef.value);
    });

    return {
      loginForm,
      loginFormRef,
      rules,
      submitForm,
      submitting,
    };
  },
});
</script>
<style lang="scss" scoped>
.login-container {
  width: 100vw;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
}
</style>
